package com.zc.im.service.im.group.impl;

import com.zc.im.mapper.im.group.ZcImGroupMemberMapper;
import com.zc.im.model.im.group.ZcImGroupMember;
import com.zc.im.model.im.group.ZcImGroupMemberExample;
import com.zc.im.model.im.group.ZcImGroupMemberKey;
import com.zc.im.service.im.group.ImGroupMemberService;
import com.zc.im.util.ImGolbalTypeContext;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 群表
 * Created by wangcc on 2017/7/8.
 */
@Service
public class ImGroupMemberServiceImpl implements ImGroupMemberService {

    @Autowired
    private ZcImGroupMemberMapper groupMemberMapper;
    private static Logger logger = Logger.getLogger(ImGroupMemberServiceImpl.class);


    /**
     * 建立用户关联群
     * @param zcImGroupMemberModel
     * @return
     */
    @Override
    public Integer insertGroupMember(ZcImGroupMember zcImGroupMemberModel) {
        Integer add = null;
        if (zcImGroupMemberModel == null) {
            logger.info("add GroupMember is null");
            return null;
        }
        zcImGroupMemberModel.setCreatedTime(new Date());
        zcImGroupMemberModel.setStatus(ImGolbalTypeContext.STATUS_VALID);
        try {
            add = groupMemberMapper.insertSelective(zcImGroupMemberModel);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return add;
    }

    /**
     * 删除用户与群的对应关系
     * @param id
     * @return
     */
    @Override
    public Integer deleteGroupMember(Integer id) {
        Integer groupMember = null;
        if (id == null) {
            logger.info("delete GroupMember is null");
            return null;
        }
        try {
            ZcImGroupMemberKey key = new ZcImGroupMemberKey();
            key.setId(id);
            groupMember = groupMemberMapper.deleteByPrimaryKey(key);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return groupMember;
    }


    /**
     * 更新用户与群的对应关系
     * @param zcImGroupMember
     * @return
     */
    @Override
    public Integer updateGroupMember(ZcImGroupMember zcImGroupMember) {
        Integer udg = null;
        if (zcImGroupMember == null) {
            logger.info("update GroupMember is null ");
            return null;
        }
        zcImGroupMember.setUpdatedTime(new Date());
        try {
            ZcImGroupMemberExample memberExample = new ZcImGroupMemberExample();
            ZcImGroupMemberExample.Criteria criteria = memberExample.createCriteria();
            criteria.andIdEqualTo(zcImGroupMember.getId());
            udg = groupMemberMapper.updateByExample(zcImGroupMember, memberExample);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return udg;
    }


    /**
     * 查询群对应的人员
     * @param zcImGroupMemberModel
     * @return
     */
    @Override
    public List<ZcImGroupMember> queryAllGroupMember(ZcImGroupMember zcImGroupMemberModel) {
        List<ZcImGroupMember> zcImGroupMemberModels = new ArrayList<>();
        if (zcImGroupMemberModel == null) {
            logger.info("getall GroupMember is null");
            return zcImGroupMemberModels;
        }
        try {
            ZcImGroupMemberExample memberExample = new ZcImGroupMemberExample();
            ZcImGroupMemberExample.Criteria criteria = memberExample.createCriteria();
            criteria.andGroupIdEqualTo(zcImGroupMemberModel.getGroupId());

            zcImGroupMemberModels = groupMemberMapper.selectByExample(memberExample);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        return zcImGroupMemberModels;
    }
}
